//
//  UBContentVerticalView.m
//  CoolJF
//
//  Created by zibin on 2017/3/30.
//  Copyright © 2017年 ubi. All rights reserved.
//

#import "ZBContentVerticalView.h"
//默认间距
#define UBContentVerticalView_OffsetVerticalHeight  10
//默认字体
#define UBContentVerticalView_titleLabelFont  [UIFont systemFontOfSize:13.0]
//默认颜色
#define UBContentVerticalView_titleLabelColor  [UIColor blackColor]
@interface ZBContentVerticalView()
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) UIOffset titleOffset;
@property (nonatomic, strong) UIFont *titleFont;
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) CGSize contentVerticalViewSize;
@end

@implementation ZBContentVerticalView
- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title
{
    _titleOffset = UIOffsetMake(0, UBContentVerticalView_OffsetVerticalHeight);
    return [self initWithImage:image title:title offset:_titleOffset];
}
- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title offset:(UIOffset)offset
{
    _titleFont = UBContentVerticalView_titleLabelFont;
    return [self initWithImage:image title:title titleFont:_titleFont offset:offset];
}
- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title titleFont:(UIFont *)font offset:(UIOffset)offset
{
    _titleColor = UBContentVerticalView_titleLabelColor;
    return [self initWithImage:image title:title titleFont:font titleColor:_titleColor offset:offset];
}
- (instancetype)initWithImage:(UIImage *)image title:(NSString *)title titleFont:(UIFont *)font titleColor:(UIColor *)titleColor offset:(UIOffset)offset
{
    if(self = [super init])
    {
        if(image == nil) return nil;
        if(title.length == 0)return nil;
        self.image = image;
        self.title =title;
        self.titleOffset = offset;
        self.titleFont = font;
        self.titleColor = titleColor;
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        //赋值
        self.imageView.image = image;
        self.titleLabel.text = title;
        //计算高度
        [self computeSize];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
    self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+self.titleOffset.vertical, self.contentVerticalViewSize.width, self.contentVerticalViewSize.height -CGRectGetMaxY(self.imageView.frame)-self.titleOffset.vertical);
    
}

- (void)configImage:(UIImage *)image title:(NSString *)title
{
    if(image == nil) return;
    if(title.length == 0)return;
    //赋值
    self.imageView.image = image;
    self.titleLabel.text = title;
    //计算高度
    [self computeSize];
    [self setNeedsLayout];
}


- (void)computeSize
{
    //获取图片的宽高
    CGSize imageSize = self.image.size;
    //文字宽高
    CGSize titleSize = [self.title sizeWithAttributes:@{NSFontAttributeName : self.titleFont}];
    //计算的宽高
    CGFloat width = MAX(imageSize.width, titleSize.width);
    CGFloat height = imageSize.height +titleSize.height + self.titleOffset.vertical;
    self.contentVerticalViewSize = CGSizeMake(width, height);
}


- (CGSize)getContentVerticalViewSize
{
    return self.contentVerticalViewSize;
}



#pragma mark - lazy
- (UIImageView *)imageView
{
    if(_imageView == nil)
    {
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
    }
    return _imageView;
}

-(UILabel *)titleLabel
{
    if(_titleLabel == nil)
    {
        _titleLabel = [UILabel new];
        _titleLabel.font = self.titleFont;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = self.titleColor;
    }
    return _titleLabel;
}

@end
